import React, { useCallback } from 'react';
// import dva,{connect} from 'dva'
import dva,{connect} from './dva'
import{Router,Route} from './dva/router'
// import{Router,Route} from 'dva/router'
const app = dva()
const delay =ms=>new Promise(function(resolve){
  setTimeout(()=>{resolve();},ms)
})
app.model({
  namespace:'counter',
  state:{number:0},
  reducers:{
    add(state){
      return {number:state.number+1}
    }
  },
    effects:{
      *asyncAdd(action,{put,call}){
        yield call(delay,1000);
        yield put({type:'counter/add'})
      }
    }
})

let Counter = connect(
  state => state.counter
)(
  props=>(
    <div>
      <p>{props.number}</p>
      <button onClick={()=>props.dispatch({type:'counter/add'})}>+</button>
      <button onClick={()=>props.dispatch({type:'counter/asyncAdd'})}>异步加1</button>
      <button onClick={()=>props.dispatch({type:'counter/asyncAdd'})}>跳转到/路径</button>
    </div>
  )
)
const Home = props=><div>首页</div>
app.router(({history})=>(
  <Router history={history}>
    <>
      <Router path='/' exact component={Home}/>
      <Router path='/counter' exact component={Counter}/>
    </>
  </Router>

))
// app.router(()=><Counter/>)


app.start('#root')